<?php

class myTools
{
    const STRIP_LENGTH = 10;
    const STRIP_TRAIL = '...';

    public static function debug($var, $die = true)
    {
        echo '<pre>';
        var_dump($var);
        echo '</pre>';
        if ($die)
        {
            die();
        }
    }

    public static function getCurrentDate()
    {
        return date(sfConfig::get('php_mysql_date_format'));
    }

    public static function encryptPassword($password)
    {
        return md5($password);
    }

    /**
     * Equivalent to explode(' ', $var)
     *
     * @param string|array $var
     * @return array
     */
    public static function explode($var)
    {
        if (!is_array($var))
        {
            $var = explode(' ', $var);
        }
        $list = array();

        foreach ($var as $value)
        {
            $value = trim($value);
            if (strlen($value))
            {
                $list[] = $value;
            }
        }

        return $list;
    }

    public static function strip($string, $length = self::STRIP_LENGTH)
    {
        if (strlen($string) > $length)
        {
            $string = substr($string, 0, $length) . self::STRIP_TRAIL;
        }

        return $string;
    }

    public static function htmlImplode($array)
    {
        return implode('<br />', $array);
    }

    public static function find_duplicates($array, $unique = false)
    {
        $duplicates = array_diff_assoc($array, array_unique($array));
        if ($unique)
        {
            $duplicates = array_unique($duplicates);
        }

        return $duplicates;
    }

    protected static $sort_key;
    protected static $sort_order;
    const SORT_ASC = 1;
    const SORT_DESC = -1;

    protected static function _sortByKey(array $a, array $b)
    {
        return self::$sort_order * strnatcasecmp($a[self::$sort_key], $b[self::$sort_key]);
    }

    public static function sortByKey(&$array, $key, $sort = self::SORT_ASC, $preserve_keys = true)
    {
        self::$sort_key = $key;
        if ($sort === null)
        {
            $sort = self::SORT_ASC;
        }
        self::$sort_order = $sort;
        if ($preserve_keys)
        {
            uasort($array, array(__CLASS__, '_sortByKey'));
        }
        else
        {
            usort($array, array(__CLASS__, '_sortByKey'));
        }
    }

    /**
     * Trim by reference
     * @param mixed $val
     */
    public static function trim(&$val)
    {
        $val = trim($val);
    }

    /**
     * Transforma diacriticile in litere latine
     * @param string $string
     * @return string
     */
    public static function toLatin($string)
    {
        return iconv('UTF-8', 'ASCII//TRANSLIT', $string);
    }

    /**
     * Returneaza valorile din $input ale caror chei sunt specificate in $keys.
     * Ordinea noului array va fi data de $keys
     *
     * @param array $input
     * @param array $keys
     * @return array
     */
    public static function arrayFilterKeys(array $input, array $keys)
    {
        $array = array();
        foreach ($keys as $key)
        {
            $value = $input[$key];
            if (isset($value))
            {
                $array[$key] = $value;
            }
        }

        return $array;
    }

    static public function array_equal(array $a, array $b)
    {
        // compare array lengths
        if (count($a) != count($b))
        {
            return false;
        }

        // compare each element
        foreach ($a as $index => $element)
        {
            $pos = array_search($element, $b);
            if ($pos === false)
            {
                return false;
            }
            else
            {
                unset($a[$index], $b[$pos]);
                continue;
            }
        }

        return true;
    }
    
}
